package cn.com.yiqi.wsc.service.org.impl;

import cn.com.yiqi.wsc.Ro.OrgVO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.dao.org.OrgDao;
import cn.com.yiqi.wsc.dao.org.OrgExtendDao;
import cn.com.yiqi.wsc.dao.org.OrgLinkDao;
import cn.com.yiqi.wsc.dao.org.criteria.AdminOrgCriteria;
import cn.com.yiqi.wsc.dao.user.UserDao;
import cn.com.yiqi.wsc.entity.check.Check;
import cn.com.yiqi.wsc.entity.org.OrgAdminDTO;
import cn.com.yiqi.wsc.entity.org.OrgEntity;
import cn.com.yiqi.wsc.entity.org.OrgExtendEntity;
import cn.com.yiqi.wsc.entity.org.OrgLinkEntity;
import cn.com.yiqi.wsc.enumeration.*;
import cn.com.yiqi.wsc.service.check.CheckService;
import cn.com.yiqi.wsc.service.org.OrgService;
import cn.com.yiqi.wsc.service.user.UserService;
import cn.com.yiqi.wsc.service.user.dataobject.UserDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 何刚
 * @ClassName: OrgServiceImpl
 * @Description: TODO
 * @date 2017/2/24
 */
@Service
public class OrgServiceImpl implements OrgService {

    @Autowired
    private OrgDao orgDao;

    @Autowired
    private OrgLinkDao orgLinkDao;

    @Autowired
    private OrgExtendDao orgExtendDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserService userService;

    @Autowired
    private CheckService checkService;


    @Value("#{APP_PROP['user.initPassword']}")
    private String initPassword;

    @Override
    public Result adminOrgInfoList(AdminOrgCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        PageHelper.startPage(criteria.getCurrentPage(), criteria.getPageSize());
        PageHelper.orderBy("so.d_insert_time DESC");
        List<OrgAdminDTO> orgAdminDTOs = orgDao.adminOrgInfoList(criteria);
        PageInfo<OrgAdminDTO> pageInfo = new PageInfo<>(orgAdminDTOs);
        result.putData("list", pageInfo.getList());
        result.putData("total", pageInfo.getTotal());

        return result;
    }

    @Transactional
    @Override
    public Result insert(OrgEntity entity, OrgExtendEntity orgExtends, List<OrgLinkEntity> orgLinkList, Integer userId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        //设置删除默认值
        entity.setDelete(CommonEnum.PRESERVE.getValue());
        entity.setCharacter(orgExtends.getCharacter());
        entity.setEstablishTime(orgExtends.getEstablishTime());
        entity.setPointProportion(orgExtends.getPointProportion());
        entity.setInsertUserId(userId);
        entity.setEnable(CommonEnum.ENABLE.getValue());
        orgDao.insert(entity);

        orgExtends.setInsertUserId(userId);
        orgExtends.setOrgId(entity.getId());
        //默认值
        orgExtends.setSaleAmount(new BigDecimal(0));
        //默认值
        orgExtends.setSaleCount(0);
        orgExtendDao.insert(orgExtends);

        if (orgLinkList != null) {
            for (OrgLinkEntity orgLink : orgLinkList) {
                orgLink.setUserId(userId);
                orgLink.setOrgId(entity.getId());
                orgLinkDao.insert(orgLink);
            }
        }

        return result;
    }

    @Override
    public Result update(OrgEntity entity, Integer userId) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        entity.setUpdateUserId(userId);

        orgDao.update(entity);

        return result;
    }

    @Override
    public Result detail(Integer orgId) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        OrgEntity orgEntity = new OrgEntity();
        orgEntity.setId(orgId);

        orgEntity = orgDao.get(orgEntity);

        result.putData("baseInfo", orgEntity);

        AdminOrgCriteria criteria = new AdminOrgCriteria();
        criteria.setOrgId(orgId);

        OrgExtendEntity orgExtendEntity = orgExtendDao.getDetailExtendByOrgId(criteria);

        if (null != orgExtendEntity) {
            orgExtendEntity.setPointProportion(orgEntity.getPointProportion());
            orgExtendEntity.setCharacter(orgEntity.getCharacter());
            orgExtendEntity.setEstablishTime(orgEntity.getEstablishTime());
        }

        if (null == orgExtendEntity) {
            orgExtendEntity = new OrgExtendEntity();
        }

        result.putData("extend", orgExtendEntity);

        result.putData("linkList", orgLinkDao.getOrgLinkListByOrgId(criteria));

        result.putData("accountList", userDao.getUserByOrgId(orgId));

        return result;
    }

    @Override
    public Result delete(Integer orgId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());


        OrgEntity orgEntity = new OrgEntity();
        orgEntity.setId(orgId);
        orgEntity.setDelete(CommonEnum.DELETED.getValue());

        return result;
    }

    @Override
    public Result insertExtend(OrgExtendEntity extendEntity) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        orgExtendDao.insert(extendEntity);

        return result;
    }

    @Transactional
    @Override
    public Result updateExtend(OrgExtendEntity extendEntity, Integer userId) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        OrgEntity orgEntity = new OrgEntity();
        orgEntity.setId(extendEntity.getOrgId());
        orgEntity.setCharacter(extendEntity.getCharacter());
        orgEntity.setEstablishTime(extendEntity.getEstablishTime());
        orgEntity.setUpdateUserId(userId);

        orgDao.update(orgEntity);

        orgExtendDao.update(extendEntity);

        return result;
    }

    @Override
    public Result getDetailExtendByOrgId(Integer orgId) {


        AdminOrgCriteria criteria = new AdminOrgCriteria();
        criteria.setOrgId(orgId);
        orgExtendDao.getDetailExtendByOrgId(criteria);

        return null;
    }

    @Override
    public Result getOrgLinkListByOrgId(Integer orgId) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        AdminOrgCriteria criteria = new AdminOrgCriteria();
        criteria.setOrgId(orgId);

        List<OrgLinkEntity> orgLinkEntities = orgLinkDao.getOrgLinkListByOrgId(criteria);
        result.putData("orgLinkList", orgLinkEntities);

        return result;
    }

    @Override
    public Result insertLink(OrgLinkEntity entity, Integer userId) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        entity.setInsertUserId(userId);
        orgLinkDao.insert(entity);

        return result;
    }

    @Override
    public Result updateLink(OrgLinkEntity entity, Integer userId) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        entity.setUpdateUserId(userId);
        orgLinkDao.update(entity);

        return result;
    }

    @Override
    public Result deleteLink(OrgLinkEntity orgLinkEntity) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        orgLinkDao.delete(orgLinkEntity);
        return result;
    }

    @Override
    public Result getUserByOrgId(AdminOrgCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        result.putData("list", userDao.getUserByOrgId(criteria.getOrgId()));

        return result;
    }

    @Override
    public Result addAccount(UserDO userDO, Integer userId) {


        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        userDO.setInsertUserId(userId);
        userDO.setType(UserType.seller.getValue());
        userDO.setPassword(initPassword);
        userDO.setInsertTime(new Date());
        result = userService.insertUser(userDO);



        return result;
    }

    @Override
    public Result selectOrgById(OrgEntity orgEntity) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        result.putData("org", orgDao.selectOrgById(orgEntity));
        return result;
    }

    @Transactional
    @Override
    public Result updateOrg(OrgEntity entity, OrgExtendEntity orgExtends, OrgLinkEntity orgLink, Integer userId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        entity.setUpdateTime(new Date());
        entity.setUpdateUserId(userId);
        orgDao.update(entity);

        orgExtends.setUpdateUserId(userId);
        orgExtends.setUpdateTime(new Date());
        orgExtendDao.updateByOrgId(orgExtends);

        orgLink.setUpdateUserId(userId);
        orgLink.setUpdateTime(new Date());
        orgLinkDao.updateLinkByOrgId(orgLink);

        return result;
    }

    @Override
    public Result<List<OrgVO>> listOrgNameAndId(String name) {
        Result<List<OrgVO>> listResult = new Result<>();

        listResult.setReturnValue(orgDao.listOrgNameAndId(name));

        return listResult;
    }


    @Transactional
    @Override
    public Result check(Check check) {

        Result result = new Result<>();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        check.setContentType(CheckTypeEnum.ORG.getValue());

        OrgEntity orgEntity = new OrgEntity();
        orgEntity.setId(check.getContentId());

        if (CheckResultEnum.ACCESS.getValue() == check.getCheckResult()) {

            orgEntity.setStatus(OrgCheckStatus.PASS.getValue());

        } else {

            orgEntity.setStatus(OrgCheckStatus.REJECT.getValue());

        }

        //修改状态
        orgDao.update(orgEntity);
        //添加
        checkService.insertCheck(check);

        return result;

    }
}
